<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>
						
						<el-col :span="8">
							<el-form-item label="使用人" prop="seatName">
								<el-input v-model="tableData.param.seatName" placeholder="请输入使用人" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="8">
							<el-form-item label="电话号码" prop="phone">
								<el-input v-model="tableData.param.phone" placeholder="请输入电话号码" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="平台卡状态" prop="systemStatus">
								<el-select v-model="tableData.param.systemStatus" placeholder="请选择平台卡状态" clearable>
									<el-option v-for="dict in dictionary.list1" :key="dict.value" :label="dict.label"
										:value="dict.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="运营商卡状态" prop="authStatus">
								<el-select v-model="tableData.param.authStatus" placeholder="请选择运营商卡状态" clearable>
									<el-option v-for="dict in dictionary.list1" :key="dict.value" :label="dict.label"
										:value="dict.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="企业卡状态" prop="baseStatus">
								<el-select v-model="tableData.param.baseStatus" placeholder="请选择运营商卡状态" clearable>
									<el-option v-for="dict in dictionary.list2" :key="dict.value" :label="dict.label"
										:value="dict.value" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="!showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" link @click="toggleSearch">
									{{ word }}
									<el-icon v-show="showAll">
										<ArrowUp />
									</el-icon>
									<el-icon v-show="!showAll">
										<ArrowDown />
									</el-icon>
								</el-button>
							</el-form-item>
						</el-col>

						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" link @click="toggleSearch">
									{{ word }}
									<el-icon v-show="showAll">
										<ArrowUp />
									</el-icon>
									<el-icon v-show="!showAll">
										<ArrowDown />
									</el-icon>
								</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
				<el-row :gutter="10" class="mb8">
					<el-col :span="1.5">
						<el-button type="danger" v-if="permissionStore.isPermissionsBtn('010004001')" :disabled="multiple" @click="handle(null,'停用')">一键停用</el-button>
					</el-col>
					<el-col :span="1.5">
						<el-button type="primary" :disabled="multiple" @click="handle(null,'启用')" v-if="permissionStore.isPermissionsBtn('010004002')">一键启用</el-button>
					</el-col>
				</el-row>
			</div>
			<el-table v-loading="loading" :data="tableData.data" @selection-change="handleSelectionChange">
				<el-table-column type="selection" width="55" align="center" />
				<el-table-column label="电话号码" align="center" prop="phone" min-width="100px" />
				<el-table-column label="使用人（实名人）" align="center" prop="seatName" min-width="100px" />
				<el-table-column label="企业卡状态" align="center" prop="baseStatusTxt" min-width="100px">
					<template #default="{row}"><el-tag :type="row.baseStatusTxt=='启用'?'success':'danger'">{{ row.baseStatusTxt }}</el-tag></template>
				</el-table-column>
				<el-table-column label="平台卡状态" align="center" prop="systembaseStatusTxt" min-width="100px" />
				<el-table-column label="运营商卡状态" align="center" prop="authbaseStatusTxt" min-width="100px" />
				<el-table-column label="企业卡状态" align="center" prop="baseStatusTxt" min-width="100px" />
				<el-table-column label="操作" align="center" class-name="small-padding" min-width="200px" fixed="right">
					<template #default="scope">
							<el-button type="danger" v-if="scope.row.seatName && scope.row.status=='ENABLED' && permissionStore.isPermissionsBtn('010004003')" link @click="handle(scope.row,'停用')"><el-icon>
								<EditPen />
							</el-icon>停用</el-button>
							<el-button type="success"  v-if="scope.row.seatName && scope.row.status=='DISABLED' && permissionStore.isPermissionsBtn('010004004')" link @click="handle(scope.row,'启用')"><el-icon>
								<EditPen />
							</el-icon>启用</el-button>
					</template>
				</el-table-column>
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
	</div>
</template>
<script lang="ts">
import { phoneListApi,simcardStatusApi} from '@/api/numberManage/card';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { ElMessageBox, ElMessage, FormInstance } from 'element-plus';
import { usePermissionStore } from '@/store/modules/permission'
export default defineComponent({
	name: 'cardList', //卡号列表
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const permissionStore = usePermissionStore()
		const allocationRef=ref()
		// 是否显示所有搜索选项
		const showAll = ref(false);
		const queryRef=ref()
		const word = computed(() => {
			if (showAll.value === false) {
				//对文字进行处理
				return '展开搜索';
			} else {
				return '收起搜索';
			}
		});
		const state = reactive({
			ids: [],
			seatUserList:[],
			dictionary:{
				"ENABLED":"启用",
				"SUSPENDED":"欠费停机",
				"DISABLED":"禁用",
				list1:[
					{
						value:'ENABLED',
						label:'启用'
					},
					{
						value:'DISABLED',
						label:'禁用'
					}
				],
				list2:[
					{
						value:'ENABLED',
						label:'启用'
					},
					{
						value:'SUSPENDED',
						label:'欠费停机'
					},
					{
						value:'DISABLED',
						label:'禁用'
					}
				]
			},
			multiple: true,
			tableData: {
				data: [],
				total: 0,
				loading: false,
				dateRange: [],
				param: {
					pageNumber: 1,
					pageLength: 10,
					seatName:undefined,
					phone:undefined,
					systemStatus:undefined,
					authStatus:undefined,
					baseStatus:undefined,
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮操作 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			phoneListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list.map(item=>{
					item.baseStatusTxt=state.dictionary[item.baseStatus]
					item.systembaseStatusTxt=state.dictionary[item.systemStatus]
					item.authbaseStatusTxt=state.dictionary[item.authStatus]
					item.baseStatusTxt=state.dictionary[item.baseStatus]
					return item
				})
				console.log(state.tableData.data);
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};

	
		
		const toggleSearch = () => {
			showAll.value = !showAll.value;
		};
		// 多选框选中数据
		const handleSelectionChange = (selection) => {
			state.ids = selection.map((item) => item.id);
			state.multiple = !selection.length;
		};
		const handle = (row,type) => {
			let msg =`你确定要${type}所选数据？`;
			let id: number[] = [];
			if (row) {
				msg = `此操作将 "${type}" 账号: "${row.phone}"，是否继续?`;
				id = [row.id];
			} else {
				id = state.ids;
			}
			if (id.length === 0) {
				ElMessage.error(`请选择要${type}的数据。`);
				return;
			}


			ElMessageBox.confirm(msg, '提示', {
				confirmButtonText: '确认',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(async() => {
					if(type=='停用'){
						await simcardStatusApi({ids:id,status:'DISABLED'})
					}else if(type=='启用'){
						await simcardStatusApi({ids:id,status:'ENABLED'})
					}
					ElMessage.success(`${type}成功`);
					getList();
				})
				.catch(() => { });
		};
		
		return {
			proxy,
			showAll,
			loading,
			word,
			queryRef,
			allocationRef,
			resetQuery,
			getList,
			toggleSearch,
			handleSelectionChange,
			handle,
			
			permissionStore,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
